<?php

namespace Acme\CommonBundle\Service;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Acme\UserBundle\Helper\UploadHandler;
use Acme\PersonBundle\Entity\Personimage;
use Acme\PinnwandBundle\Entity\Pinnwand;
use Acme\PinnwandBundle\Entity\Pinnwandgalerie;
use Acme\EventBundle\Entity\Information;
use Acme\EventBundle\Entity\Feedback;
use Doctrine\ORM\EntityRepository;

class Pagedata {
	private $context;
	private $em;
	public function __construct(SecurityContext $context,\Doctrine\ORM\EntityManager $em) {
		$this->context = $context;
		$this->em = $em;
	}
	public function getCommonSecurity() {
		$arr_data = array ();
		$usr = $this->getUser ();
		// $em = $this->getDoctrine()->getManager();
		$connection = $this->getEntityManager ()->getConnection ();
		$statement = $connection->prepare ( "
			SELECT IFNULL( pi.path, 'bundles/acmeperson/images/') AS path, IFNULL( pi.image , 'face-std.jpg' ) AS image FROM users u
   				LEFT JOIN persons p ON p.id = u.id
   				LEFT JOIN personimages pi ON p.id=pi.person_id AND pi.standart=1
    			WHERE u.id = :id
				group by p.id
    			" );
		$statement->bindValue ( 'id', $usr->getId () );
		$statement->execute ();
		$arr_data ["usr"] = $usr;
		$arr_data ["personimagest"] = $statement->fetchAll ();
		
		$arr_data ["personimages"] = $this->getEntityManager ()->getRepository ( 'AcmeUserBundle:User' )->findPersonImagesByUserId ( $usr->getId () );
		
		$arr_data ["persons"] = $this->getEntityManager ()->getRepository ( 'AcmePersonBundle:Person' )->find ( $usr->getId () );
		
		$arr_data ["usersinformation"] = $this->getUsersInformation ();
		
		$current_month = date ( 'n', time () );
		$arr_data ["inscriptions"] = $this->getEntityManager ()->getRepository ( 'AcmeEventBundle:Inscription' )->findCurrentInscription ();
		
		if (count ( $arr_data ["inscriptions"] ) > 0) {
			$arr_data ["information_datum"] = $arr_data ["inscriptions"] [0] ["datum"];
		} else {
			$arr_data ["information_datum"] = "";
		}
		
		// $arr_data["inscriptions"] = '';
		// $temp_time = 0;
		// foreach($arr_inscription as $inscription){
		// if($inscription['datum_send']->getTimestamp() <= time() && $inscription['is_send']){
		// if($inscription['datum_send']->getTimestamp() >= $temp_time){
		// $temp_time = $inscription['datum_send']->getTimestamp();
		// $arr_data["inscriptions"][] = $inscription;
		// //echo $inscription["datum"]->format('d.m.Y'); exit;
		// $arr_data["information_datum"] = $inscription["datum"];
		// }
		// }
		// }
		return $arr_data;
	}
	private function getUsersInformation() {
		$users = $this->getEntityManager ()->getRepository ( 'AcmeUserBundle:User' )->findAll ();
		$usr = $this->getUser ();
		$all_users = array ();
		foreach ( $users as $user ) {
			// echo $user->getLastActivity()->format(); exit;
			if ($usr->getId () != $user->getId ()) {
				$person = $this->getEntityManager ()->getRepository ( 'AcmePersonBundle:Person' )->find ( $user->getId () );
				$personimages = $this->getEntityManager ()->getRepository ( 'AcmePersonBundle:Personimage' )->findByPerson ( $user->getId () );
				
				$isStandart = false;
				foreach ( $personimages as $personimage ) {
					if ($personimage->getStandart ()) {
						$all_users ["" . $user->getId ()] ["file"] = '/' . $personimage->getPath () . 'thumbnail/' . $personimage->getImage ();
						$isStandart = true;
					}
				}
				if (! $isStandart) {
					$all_users ["" . $user->getId ()] ["file"] = '/bundles/acmeperson/images/face-std-thumbnail.jpg';
				}
				
				$all_users ["" . $user->getId ()] ["firstname"] = $person->getFirstname ();
				if (time () - strtotime ( $user->getLastActivity ()->format ( 'Y-m-d H:i:s' ) ) <= 120) {
					// aktiv
					$all_users ["" . $user->getId ()] ["status"] = true;
				} else {
					// inaktiv
					$all_users ["" . $user->getId ()] ["status"] = false;
				}
			}
		}
		
		return $all_users;
	}
	private function getUser() {
		return $this->context->getToken ()->getUser ();
	}
	private function getEntityManager() {
		return $this->em;
	}
}